$(function () {
    initLists()
    let layer = layui.layer
    function initLists() {

        axios({
            method: 'GET',
            url: '/my/article/cates',

        }).then(res => {
            // console.log(res);
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            let arr = []
            res.data.data.forEach(ele => {
                arr.push(`
                <tr>
                    <td>${ele.Id}</td>
                    <td>${ele.name}</td>
                    <td>${ele.alias}</td>
                    <td>
                        <button data-id="${ele.Id}" type="button" class="layui-btn layui-btn-xs xiuGai">编辑</button>
                        <button data-id="${ele.Id}" type="button" class="shanChu layui-btn layui-btn-xs  layui-btn-danger">删除</button>
                    </td>
                </tr>

                `)
            })
            $('tbody').empty().html(arr.join(''))
        })

    }
    let indexAdd = 0
    $('#tianJia').on('click', function () {
        indexAdd = layer.open({
            type: 1,
            title: '添加文字类别',
            area: ['500px', '260px'],
            content: `
            <form id="indexForm" class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">分类名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">分类别名</label>
                    <div class="layui-input-block">
                        <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn xiuGai" lay-submit lay-filter="formDemo">确认修改</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
                
            `,

        });
    })
    $('body').on('submit', '#indexForm', function (e) {
        e.preventDefault()
        axios({
            method: 'post',
            url: '/my/article/addcates',
            data: $(this).serialize()
        }).then(res => {
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            layer.msg('恭喜您,添加文章类别成功!')
            initLists()
            layer.close(indexAdd)
        })
    })
    let indexEdit = 0
    $('tbody').on('click', '.xiuGai', function () {
        indexEdit = layer.open({
            type: 1,
            title: '修改文字类别',
            area: ['500px', '260px'],
            content: `
            <form id="formEdit" class="layui-form" lay-filter="formEdit">
                <input type="hidden" name="Id">
                <div class="layui-form-item">
                    <label class="layui-form-label">分类名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">分类别名</label>
                    <div class="layui-input-block">
                        <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
                
            `,

        });
        let id = $(this).attr('data-id')
        axios({
            url: '/my/article/cates/' + id,
        }).then(res => {
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            layui.form.val('formEdit', res.data.data)
        })

    })
    $('body').on('submit', '#formEdit', function (e) {
        e.preventDefault()
        axios({
            method: 'post',
            url: '/my/article/updatecate',
            data: $(this).serialize()
        }).then(res => {
            console.log(res);
            if (res.data.status !== 0) {
                return layer.msg(res.data.message)
            }
            layer.msg('恭喜您,修改文章类别成功!')
            initLists()
            layer.close(indexEdit)
        })
    })
    $('tbody').on('click', '.shanChu', function () {
        let id = $(this).attr('data-id')
        layer.confirm('确定删除嘛?', { icon: 3, title: '提示' }, function (index) {
            //do something
            axios({
                url: '/my/article/deletecate/' + id,
            }).then(res => {
                if (res.data.status !== 0) {
                    return layer.msg(res.data.message)
                }
                layer.msg('恭喜您,删除文章成功!')
                initLists()
            })
            layer.close(index);
        });

    })
})